//index.js
//获取应用实例
const app = getApp()
Page({
  data: {
    motto: '',
    userInfo: {},
    hasUserInfo: false,
    canIUse: wx.canIUse('button-type.getUserInfo'),
    childList: [],
    childName: '',
    parentMobile: '',
    childRelation:'',
    relation: [
      { value: "0", name: "请选择" },
      { value: "1", name: '爸爸' },
      { value: "2", name: '妈妈' },
      { value: "3", name: '亲戚' },
      { value: "4", name: '朋友' }
    ],
    relation_index: 0
  },
  //事件处理函数
  bindViewTap: function () {

  },
  onLoad: function () {
    
  },
  onShow: function() {
    if (!app.isLogin()) {
      this.getUserInfo();
    } else {
      this.queryBindChild();
    }
  },
  queryBindChild: function () {
    this.setData({
      appUser: app.globalData.appUser,
      isManager: app.globalData.appUser.teacherInfo && app.globalData.appUser.teacherInfo.isManager == 1 ? true : false
    })
    let that = this;
    wx.request({
      url: app.httpUrl + '/user/queryBindChild',
      header: {
        'xcx-xtoken': app.globalData.openid,
        'content-type': 'application/json' // 默认值
      },
      method: "GET",
      success: (res) => {
        var childList = res.data;
        var currTime = new Date().getTime();
        for(var i in childList) {
          var startDate = childList[i].activityStartDate + 24*60*60*1000;
          var endDate = childList[i].activityEndDate + 24*60*60*1000;
          if (currTime > startDate && currTime < endDate) {
            childList[i].isSign = true;
          }
        }
        that.setData({ childList: childList });
      }
    })
  },
  addchild: function () {
    this.setData({
      showModalStatus: true //让模态框显示
    });
  },
  bindPickerChange_relation: function (e) {
    var relation_index = this.data.relation_index;
    var childRelation = e.detail.value;
    for(var i in this.data.relation) {
      if (this.data.relation[i].value == childRelation) {
        relation_index = i;
        break;
      }
    }
    this.setData({
      childRelation: e.detail.value,
      relation_index: relation_index
    })
  },
  // form表单
  formSubmit: function (e) {
    var childName = e.detail.value.childName;
    if (childName == '') {
      wx.showToast({ title: '宝贝姓名不能为空', icon: 'none', duration: 1000 });
      return;
    }
    var parentMobile = e.detail.value.parentMobile;
    if (parentMobile == '') {
      wx.showToast({ title: '家长报名电话不能为空', icon: 'none', duration: 1000 });
      return;
    }
    if (this.data.childRelation == '0' || this.data.childRelation == '') {
      wx.showToast({ title: '请选择您和宝贝的关系哦！', icon: 'none', duration: 1000 });
      return;
    }
    wx.request({
      url: app.httpUrl + '/user/addParentBindChild',
      header: {
        'xcx-xtoken': app.globalData.openid,
        'content-type': 'application/json' // 默认值
      },
      method: "POST",
      data: {
        childName: childName,
        parentMobile: parentMobile,
        childRelation: this.data.childRelation,
      },
      success: (res) => {
        if (res.data.code != '000') {
          var msg = res.data.code == '1026' && res.data.data != null ? '请用您提供给我们的手机号码绑定：' + res.data.data : res.data.msg;
          wx.showToast({ title: msg, icon: 'none', duration: 5000 });
        } else {
          wx.showToast({ title: '绑定成功', icon: 'success', duration: 3000 });
          this.setData({
            childName: '',
            parentMobile: '',
            childRelation: '',
            relation_index: '0',
            showModalStatus: false //让模态框显示
          })
          this.queryBindChild();
        }
      }
    })
  },
  unbindChild: function(event) {
    var id = event.currentTarget.dataset.id;
    var name = event.currentTarget.dataset.name;
    var that = this;
    wx.showModal({
      title: '解绑宝贝',
      content: '确认解除与' + name + '宝贝之间的绑定?',
      success: function (res) {
        if (res.confirm) {
          wx.request({
            url: app.httpUrl + '/user/unBindChild',
            header: {
              'xcx-xtoken': app.globalData.openid,
              'content-type': 'application/json' // 默认值
            },
            data: {
              id: id
            },
            method: "POST",
            success: (res) => {
              if (res.data.code == '000') {
                wx.showToast({ title: '解绑成功！', icon: 'success', duration: 2000 });
                that.queryBindChild();
                return false;
              }
              wx.showToast({ title: "解绑失败！", icon: 'none', duration: 3000 });
            }
          })
        } else if (res.cancel) {
         
        }
      }
    })
  },
  formReset: function () {
    this.setData({
      childName: '',
      parentMobile: '',
      relation_index: '0',
      childRelation: '',
      showModalStatus: false //让模态框显示
    })
  },
  previewImage: function (event) {
    var src = event.currentTarget.dataset.src; //获取data-src
    var list = [src];
    wx.previewImage({
      current: src, // 当前显示图片的http链接 
      urls: list // 需要预览的图片http链接列表     
    })
  },
  getUserInfo: function () {
    let that = this;
    if (app.globalData.appUser != null) {
      return;
    }
    wx.login({
      success: function (res) {
        console.log("---res.code=" + res.code);
        if (res.code) {
          //发起网络请求
          wx.request({
            url: app.httpUrl + '/common/getUserInfo',
            data: {
              code: res.code
            },
            header: {
              'content-type': 'application/json' // 默认值
            },
            method: "POST",
            success: function (res) {
              console.log(res)
              if (res.data.code == '000') {
                app.globalData.appUser = res.data.data;
                app.globalData.openid = res.data.data.openid;
                that.queryBindChild();
              }
            }
          })
        } else {
          console.log('登录失败！' + res.errMsg)
        }
      }
    });
  }
})
